package cn.hy.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

@Service
public class MessageServiceImpl implements MessageService{

    @Override
    @Transactional
    public String getMessage() {
        return "hello world";
    }


    @Resource
    private TransactionTemplate template;


    @Override
    public void setMessage(String message) {

        //  @Transactional 声明式无关系哦
        template.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                System.out.println(message);
                return null;
            }
        });

    }

    @Override
    @Transactional(transactionManager = "myTxPlatformTransactionManager")
    public String customTxManagerMessage() {
        throw new RuntimeException("11");
        //return "customer";
    }
}
